package pers.qianyu.month_202011.date_20201117;

/**
 * 213. 打家劫舍 II
 * https://leetcode-cn.com/problems/house-robber-ii/
 *
 * @author mizzle rain
 * @date 2020-11-17 21:55
 */
public class RobTwo {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        int a = 0, b = nums[0];
        for (int i = 1; i < nums.length - 1; i++) {
            int temp = Math.max(b, a + nums[i]);
            a = b;
            b = temp;
        }
        int c = nums[1];
        a = 0;
        for (int i = 2; i < nums.length; i++) {
            int temp = Math.max(c, a + nums[i]);
            a = c;
            c = temp;
        }
        return Math.max(b, c);
    }

    public static void main(String[] args) {
        int result = new RobTwo().rob(new int[]{1, 2, 3, 1});
        System.out.println("result = " + result);
    }
}
